//
//  NSString+Amount.m
//  CheFu365
//
//  Created by safiri on 2018/4/25.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "NSString+Amount.h"

@implementation NSString (Amount)

+ (NSString *)amount:(NSString *)number {
    if(number == nil || number.length == 0){
        return @"0.00";
    }
    
    if ([number doubleValue] > 0) {
        number = [NSString stringWithFormat:@"%.2f",[number doubleValue]];
        NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc]init];
        numFormatter.numberStyle = NSNumberFormatterDecimalStyle;
        
        NSNumber *num = [NSNumber numberWithDouble:[number doubleValue]];
        NSString *str = [numFormatter stringFromNumber:num];
        if ([str rangeOfString:@"."].length > 0) {
            NSString *floatStr = [str componentsSeparatedByString:@"."][1];
            if (floatStr.length == 1) {
                str = [NSString stringWithFormat:@"%@0",str];
            }else {
                str = [str substringToIndex:([str rangeOfString:@"."].location + 3)];
            }
            return [NSString stringWithFormat:@"%@",str];
        }else {
            return [NSString stringWithFormat:@"%@.00",str];
        }
    }else {
        return @"0.00";
    }
}
+ (NSString *)amountDouble:(double)number {
    
    if (number > 0) {
        NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc]init];
        numFormatter.numberStyle = NSNumberFormatterDecimalStyle;
        
        NSNumber *num = [NSNumber numberWithDouble:number];
        NSString *str = [numFormatter stringFromNumber:num];
        if ([str rangeOfString:@"."].length > 0) {
            NSString *floatStr = [str componentsSeparatedByString:@"."][1];
            if (floatStr.length == 1) {
                str = [NSString stringWithFormat:@"%@0",str];
            }else {
                str = [str substringToIndex:([str rangeOfString:@"."].location + 3)];
            }
            return [NSString stringWithFormat:@"%@",str];
        }else {
            return [NSString stringWithFormat:@"%@.00",str];
        }
    }else {
        return @"0.00";
    }
}


#define ZSDiv 10000
#define ZSUnit @[@"分", @"角"]
#define ZSLowScale @[@"",@"拾", @"佰", @"仟"]
#define ZSLargeScale @[@"",@"万",@"亿",@"兆"]
#define ZSBase @[@"零", @"壹", @"贰", @"叁", @"肆", @"伍", @"陆", @"柒", @"捌", @"玖"]

+ (NSString *)stringFamatWithCapital:(NSString *)money
{
    if (money && [money doubleValue] > 0.0f) {
        NSMutableString *capital = [[NSMutableString alloc]init];
        NSString *formatString = [NSString stringWithFormat:@"%.2f",[money doubleValue]];
        NSString *par_int = [formatString substringToIndex:formatString.length - 3];
        NSString *par_dig = [formatString substringFromIndex:formatString.length - 2];
        
        BOOL isZero = ([par_int integerValue] == 0);
        capital = [self moneyInteger:par_int];
        [capital appendString:[self moneyDecimal:par_dig isZeroInt:isZero]];
        //    if ([capital isEqualToString:@"元整"]) {
        //        capital = [NSMutableString stringWithString:@"零元整"];
        //    }
        
        return capital;
    }
    else
        return @"零元整";
}



//金额小数部分转换
+ (NSString *)moneyDecimal:(NSString *)par_dig isZeroInt:(BOOL)isZero
{
    NSMutableString *dec = [[NSMutableString alloc]init];
    NSInteger dig = [par_dig integerValue];
    if (dig == 0) {
        [dec appendString:@"元整"];
    }
    else {
        if (!isZero) {
            [dec appendString:@"元"];
        }
        //0.01 is 壹分
        NSString *temp = [NSString stringWithFormat:@"%li",(long)dig];
        if (dig % 10 != 0) {
            for (NSInteger i = 0; i < temp.length; i++) {
                [dec appendString:ZSBase[[[temp substringWithRange:NSMakeRange(i, 1)]integerValue]]];
                [dec appendString:ZSUnit[temp.length - 1 -i]];
            }
        }
        else {
            [dec appendString:ZSBase[[[temp substringWithRange:NSMakeRange(0, 1)]integerValue]]];
            [dec appendString:ZSUnit[1]];
        }
        
    }
    return dec;
}



//金额整数部分转换
+ (NSMutableString *)moneyInteger:(NSString *)par_int
{
    NSMutableString *capital = [[NSMutableString alloc]init];
    NSInteger count = -1;
    NSMutableArray *items = [NSMutableArray array];
    NSInteger lenth = par_int.length;
    long long int val_int = [par_int longLongValue];
    
    while (lenth > 0) {
        NSInteger tem = val_int % ZSDiv;
        [items addObject:[NSString stringWithFormat:@"%li",(long)tem]];
        val_int = val_int / ZSDiv;
        lenth -= 4;
        count ++;
    }
    //    NSLog(@"%@",items);
    for (NSInteger i = count; i >= 0; i --) {
        NSString *str = items[i];
        NSInteger zeroCount = 0;
        BOOL shouldZero = count > 0 && str.length < 4 && i < count;
        for (NSInteger j = 0; j < str.length; j ++) {
            NSInteger k = [[str substringWithRange:NSMakeRange(j, 1)]integerValue];
            if (k != 0) {
                if (zeroCount != 0 || shouldZero) {
                    [capital appendString:ZSBase[0]];
                    zeroCount = 0;
                    shouldZero = NO;
                }
                [capital appendString:ZSBase[k]];
                [capital appendString:ZSLowScale[str.length - 1 - j]];
            }
            else {
                zeroCount ++;
            }
        }
        if ([str integerValue] != 0) {
            [capital appendString:ZSLargeScale[i]];
        }
    }
    return capital;
}

@end
